package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.mapper.StrategyCatalogMapper;
import cn.wolfcode.trip.base.mapper.StrategyContentMapper;
import cn.wolfcode.trip.base.query.StrategyCatalogQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {
    @Autowired
    private StrategyCatalogMapper strategyCatalogMapper;
    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Override
    public PageInfo getPageInfo(StrategyCatalogQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<StrategyCatalog> list = strategyCatalogMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {
        //如果用户没有填写需要就自动生成一个
        if(strategyCatalog.getSequence()==null){
            int count = strategyCatalogMapper.getMaxSequence(strategyCatalog.getStrategy().getId());
            ++count;
            strategyCatalog.setSequence(count);
        }

        if(strategyCatalog.getId()==null){
            strategyCatalogMapper.insert(strategyCatalog);
        }else{
            strategyCatalogMapper.updateByPrimaryKey(strategyCatalog);
        }
    }

    @Override
    public List<StrategyCatalog> getStrategyCatalogByStrategyId(Long strategyId) {
        return strategyCatalogMapper.getStrategyCatalogByStrategyId(strategyId);
    }

    @Override
    public StrategyContent getStrategyContentByStrategyId(Long strategyId) {
        return strategyContentMapper.selectByPrimaryKey(strategyId);
    }

}
